package com.xuhai.blog19.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuhai.blog19.admin.dao.UserDao;
import com.xuhai.blog19.admin.entity.UserEntity;
import com.xuhai.blog19.admin.form.LoginForm;
import com.xuhai.blog19.admin.form.PasswordForm;
import com.xuhai.blog19.admin.service.CodeService;
import com.xuhai.blog19.admin.service.ExpiresService;
import com.xuhai.blog19.admin.service.UserService;
import com.xuhai.blog19.common.exception.RTException;
import com.xuhai.blog19.common.utils.R;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author PangJunjie
 * @Date 2023/2/6/006
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    @Autowired
    private CodeService codeService;
    @Autowired
    private ExpiresService expiresService;

    @Override
    public R login(LoginForm loginForm) {
        /*判断验证码是否正确*/
        if (!codeService.checkCode(loginForm.getUuid(), loginForm.getCode())) {
            return R.error("验证码不正确");
        }
        /*根据用户名获取用户信息*/
        UserEntity loginUser = getUserByUsername(loginForm.getUsername());
        if (loginUser == null || !loginUser.getPassword().equals(DigestUtils.md5Hex(loginForm.getPassword()))) {
            return R.error("用户名或密码不正确");
        }
        if (loginUser.getStatus() != 1) {
            return R.error("该用户已被封禁，请联系管理员");
        }
        return expiresService.saveToken(loginUser.getUserId());
    }

    @Override
    public void saveOrUpdateInfo(UserEntity user) {
        if (user.getUserId() == null) {
            /*验证用户名是否唯一*/
            UserEntity userEntity = getUserByUsername(user.getUsername());
            if (userEntity != null) {
                throw new RTException("用户名已存在！");
            }
            user.setPassword(DigestUtils.md5Hex("123456"));
            this.save(user);
        } else {
            this.updateById(user);
        }
    }

    @Override
    public void updatePassword(PasswordForm passwordForm) {
        UserEntity user = this.getById(passwordForm.getUserId());
        if (user == null || !user.getPassword().equals(DigestUtils.md5Hex(passwordForm.getOldPassword()))) {
            throw new RTException("原密码不正确！");
        }
        user.setPassword(DigestUtils.md5Hex(passwordForm.getPassword()));
        this.updateById(user);
    }

    @Override
    public IPage<UserEntity> getPage(int page, int limit, UserEntity user) {
        IPage<UserEntity> iPage = this.page(new Page<>(page, limit),
                new QueryWrapper<UserEntity>()
                        .eq(StringUtils.isNotBlank(user.getUsername()), "username", user.getUsername())
                        .like(StringUtils.isNotBlank(user.getNickname()), "nickname", user.getNickname())
                        .eq(user.getStatus() != null, "status", user.getStatus())
                        .orderByDesc("user_id")
        );
        return iPage;
    }


    private UserEntity getUserByUsername(String username) {
        return this.getOne(new QueryWrapper<UserEntity>().eq("username", username));
    }
}
